document.addEventListener('DOMContentLoaded', function () {
    const searchForm = document.getElementById('searchForm');
    const goodsList = document.getElementById('goodsList');

    searchForm.addEventListener('submit', function (e) {
        e.preventDefault();

        const searchQuery = document.getElementById('search_query').value;
        const searchField = document.getElementById('search_field').value;

        fetch('/search_goods', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                search_query: searchQuery,
                search_field: searchField
            })
        })
      .then(response => response.json())
      .then(data => {
            // 清空之前的商品列表
            goodsList.innerHTML = '';

            data.forEach(good => {
                const li = document.createElement('li');
                li.innerHTML = `
                    <h2>${good.name}</h2>
                    <p>Price: ${good.price}</p>
                    <p>Description: ${good.description}</p>
                    <a href="${url_for('edit_goods', id=good.id)}">编辑</a>
                `;
                goodsList.appendChild(li);
            });
        });
    });
});